package mysql

import "gorm.io/gorm"

type Product struct {
	gorm.Model
	GoodsName   string  `gorm:"type:varchar(100);not null;index"`
	GoodsPrice  float64 `gorm:"type:decimal(10,2);not null"`
	GoodsStock  int64   `gorm:"type:int;default:0"`
	GoodsImages string  `gorm:"type:varchar(100);not null;comment:商品图片"`
	Status      int64   `gorm:"type:int;default:0;comment:0代表未发布，1代表发布"`
}

func CreateProduct(Product Product) Product {
	db.Create(&Product)
	return Product
}

func RemovedGoods(Product Product) Product {
	db.First(&Product)
	Product.Status = 0
	db.Where("id = ?", Product.ID).Save(&Product)
	return Product
}

func PageOrSearch(page int64, search string) (string, []*Product) {
	var product []*Product
	var pageSize = 5
	if search != "" {
		db.Where("goods_name like?", "%"+search+"%").Limit(pageSize).Find(&product)
		return search, product
	}
	pageLimit := int(page-1) * pageSize
	db.Limit(pageLimit).Find(&product).Limit(pageSize).Find(&product)
	return "", product
}
